package pers.cz.orm.db;


import com.alibaba.druid.pool.DruidDataSource;
import pers.cz.common.annotation.TimeLag;
import pers.cz.common.connect.ConnectionFactory;
import pers.cz.common.connect.IConnection;
import pers.cz.common.connect.StoreType;
import pers.cz.orm.dao.dialect.AbstractDialect;
import pers.cz.orm.dao.dialect.DbDialect;
import pers.cz.orm.enums.DbConfigItem;
import utils.ContextUtils;
import pers.cz.jefconfig.config.JefConfiguration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: JefLogTail
 * @description: 数据源配置
 * @author: Cheng Zhi
 * @create: 2023-03-05 13:29
 **/
public class DataSourceFactory extends ConnectionFactory {

    /**
     * 保存所有的数据源
     */
    private volatile static Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>();

    @Override
    @TimeLag
    public DataSource getSource() {
        DataSource dataSource = dataSourceMap.get(ContextUtils.getDbKey());
        if (dataSource == null) {
            synchronized (DataSourceFactory.class) {
                if (dataSource == null) {
                    DataSourceFactory dataSourceFactory = new DataSourceFactory();
                    DataSource dataSourceDb1 = dataSourceFactory.getDataSourceDb1();

                    dataSourceMap.put(RouteDbEnum.MASTER.getName(), dataSourceDb1);
                    String slaveDbConf = JefConfiguration.get(DbConfigItem.SLAVE_DB_URL);
                    if (slaveDbConf != null && !"".equals(slaveDbConf)) {
                        DataSource dataSourceDb2 = dataSourceFactory.getDataSourceDb2();
                        dataSourceMap.put(RouteDbEnum.SLAVE.getName(), dataSourceDb2);
                    }
                }
                dataSource = dataSourceMap.get(ContextUtils.getDbKey());

                if (dataSource == null) {
                    // 设置为默认数据源
                    dataSource = dataSourceMap.get(RouteDbEnum.MASTER.getName());
                    ContextUtils.setDbKey(RouteDbEnum.MASTER.getName());
                }
            }
        }

        return dataSource;
    }

    /**
     * 数据源1
     * @return
     */
    @TimeLag
    public DataSource getDataSourceDb1() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setName("Master-dataSourcae");
        dataSource.setUrl(JefConfiguration.get(DbConfigItem.MASTER_DB_URL));
        dataSource.setUsername(JefConfiguration.get(DbConfigItem.MASTER_DB_USERNAME));
        dataSource.setPassword(JefConfiguration.get(DbConfigItem.MASTER_DB_PASSWORD));
        DbDialect currentDbDialect = AbstractDialect.getCurrentDbDialect(JefConfiguration.get(DbConfigItem.MASTER_DB_URL));
        dataSource.setDriverClassName(currentDbDialect.getDriveName());
        dataSource.setInitialSize(JefConfiguration.getInt(DbConfigItem.MASTER_DB_ACTIVE, 1));
        dataSource.setMaxActive(JefConfiguration.getInt(DbConfigItem.MASTER_DB_MAX_ACTIVE, 3));
        dataSource.setMinIdle(JefConfiguration.getInt(DbConfigItem.MASTER_DB_MIN_IDLE, 1));
        //dataSource.setMaxIdle(JefConfiguration.getInt(DbConfigItem.MASTER_DB_MAX_IDLE, 1));
        dataSource.setKeepAlive(JefConfiguration.getBoolean(DbConfigItem.MASTER_DB_KEEP_ALIVE, false));

        return dataSource;
    }

    /**
     * 数据源2
     * @return
     */
    @TimeLag
    public DataSource getDataSourceDb2() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setName("Slave-dataSourcae");
        dataSource.setUrl(JefConfiguration.get(DbConfigItem.SLAVE_DB_URL));
        dataSource.setUsername(JefConfiguration.get(DbConfigItem.SLAVE_DB_USERNAME));
        dataSource.setPassword(JefConfiguration.get(DbConfigItem.SLAVE_DB_PASSWORD));
        DbDialect currentDbDialect = AbstractDialect.getCurrentDbDialect(JefConfiguration.get(DbConfigItem.SLAVE_DB_URL));
        dataSource.setDriverClassName(currentDbDialect.getDriveName());
        dataSource.setInitialSize(JefConfiguration.getInt(DbConfigItem.SLAVE_DB_ACTIVE, 1));
        dataSource.setMinIdle(JefConfiguration.getInt(DbConfigItem.SLAVE_DB_MIN_IDLE, 1));
        dataSource.setMaxActive(JefConfiguration.getInt(DbConfigItem.SLAVE_DB_MAX_ACTIVE, 3));
        dataSource.setKeepAlive(JefConfiguration.getBoolean(DbConfigItem.SLAVE_DB_KEEP_ALIVE, false));
        return dataSource;
    }

}
